var rootPath = getRootPath();
var oriPhone;
var oriEmail;

$(function() {
	//滚动条置顶
	$("body").prepend('<input type="text" id="focus" size="1">');
    $("#focus").focus();
    $("#focus").hide();
    
 	var rleId = $("#rleId").val();
	var prevPage = getUrlParam("currentPage");
	
	$(".city").css("cursor", "pointer");
	//回显
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if(data.returnCode == '0000') {
				var obj = data.returnObject;
			
				$("#role").find("option[value='" + obj.sort + "']").prop("selected","selected");
				oriPhone = obj.phone;
				$("#phone").val(obj.phone);
				oriEmail = obj.email;
				$("#email").val(obj.email);
				$("#wechat").val(obj.wechat);
				$("#education").find("option[value='" + obj.education + "']").prop("selected","selected");
				getEnrollmentYear();
				$("#enrollmentYear").find("option[value='" + obj.enrollmentYear + "']").prop("selected","selected");

				$("#company").val(obj.company);
				$("#job").val(obj.job);
				
				var sortVal = $("#role").val();
				$("#sort").val(sortVal);
				changeRole();
				
				if(obj.state == 2 || obj.state == "2" ){
					$("#role").prop("disabled","disabled");
				}
				$("#school").html("<option value='" + obj.school.sid + "'>" + obj.school.sname + "</option>");
			}
		}
	});
	
	//上一步
	$("#prev").click(function() {
		//4为原来的教练计划数字
		parent.$("#content").prop("src", prevPage + "?rleId=goFarAway&creType=4&title=去远方认证");
	});
	
	//申请认证
	$("#applyAuth").click(function() {
		if(!checkData()){
			return;
		}
		//前端数据长度校验
		if($("#company").val().length > 50) {
			$("#company").focus();
			showErrMsg("company", "工作单位输入太长,请精简");
			return;
		}

		if($("#job").val().length > 50) {
			$("#job").focus();
			showErrMsg("job", "职业输入太长,请精简");
			return;
		}
		showLoading();
		$("#goFarAwayAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				if(data.returnCode == '0000'){
					layer.msg("操作成功",{icon:1,time:1000},function() {						
						parent.$("#content").prop("src","applyResult.html?rleId=goFarAway&title=去远方认证");
					});
				} else {
					layer.alert(data.returnMessage);
				}
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("申请失败,请稍后重试");
			}
		});
		return false;
	});
});

//根据不同 sort验证
function checkData(){
	var sort = $("#sort").val(); 
	
	if($.trim($("#role").val()) == '') {
		$("#role").focus();
		showErrMsg("role", "请选择角色");
		return false;
	}
	if($.trim($("#phone").val()) == '') {
		$("#phone").focus();
		showErrMsg("phone", "请输入手机号码");
		return false;
	} else {
		if($("#phone").val() != oriPhone
				&& !checkPhone($("#phone").val())) {
			$("#phone").focus();
			showErrMsg("phone","手机号码格式有误");
			return false;
		}
	}
	
	if($.trim($("#email").val()) == '') {
		if(sort && sort !=7){
			$("#email").focus();
			showErrMsg("email","请输入邮箱地址");
			return false;
		}
	} else {
		if($("#email").val() != oriEmail
				&&!checkEmail($("#email").val())) {
			$("#email").focus();
			showErrMsg("email","邮箱格式有误");
			return false;
		}
	}
	//顺序不要乱
	//学生比教师多学历 入学年份
	switch(sort){
	case"7"://学生
		$("#school").empty();
		var education=$("#education option:selected").val();
		if($.trim(education) == '') {
			$("#education").focus();
			showErrMsg("education","请选择学历");
			return false;
		}
		var enrollmentYear=$("#enrollmentYear option:selected").val();
		if($.trim(enrollmentYear) == '') {
			$("#enrollmentYear").focus();
			showErrMsg("enrollmentYear","请选择入学年份");
			return false;
		}
		//此处不需要break
	case"6"://教师
		var sid=$("#school option:selected").val();
		console.log("sid",sid)
		if($.trim(sid) == '') {
			$("#school").focus();
			showErrMsg("school","请选择学校");
			return false;
		}
		break;
	case"8":  //出行志愿者
	case"9":  //评审志愿者
		$("#school").empty();
		if($.trim($("#company").val()) == '') {
			$("#company").focus();
			showErrMsg("company","请输入工作单位");
			return false;
		}
		if($.trim($("#job").val()) == '') {
			$("#job").focus();
			showErrMsg("job","请输入职业");
			return false;
		}
		break;
	}
 
	
	 
	return true;
}